package com.wxz.gblw.cjg.controller;

import com.wxz.gblw.cjg.common.Result;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * 首页控制器
 * @Author: xiangzhong.wang
 * @Date: 2019/12/30 21:49
 */
@RestController
@RequestMapping("/photo")
public class PhotoController {

    @Value("${photo-path}")
    private String photoPath;

    @RequestMapping(value = "/getPhotoList", method = RequestMethod.GET)
    public Object getVideoList(){
        List<String> list = new ArrayList<>();
        readFiles(photoPath,list);
        Result result = new Result();
        result.setData(list);
        return result;
    }

    /**
     * 递归读取文件夹下的文件
     * @param path
     */
    public void readFiles(String path, List<String> list) {
        File file = new File(path);
        // 如果是文件夹，则需要递归处理
        if(file.isDirectory()) {
            File[] files = file.listFiles();
            for (File currentFile : files) {
                if(currentFile.isDirectory()) {
                    readFiles(currentFile.getPath(), list);
                } else  {
                    list.add(currentFile.getPath().replaceAll("\\\\","//").replaceAll(photoPath,"").replaceAll("//","/"));
                }
            }
        } else {
            list.add(file.getPath().replaceAll("\\\\","//").replaceAll(photoPath,"").replaceAll("//","/"));
        }

    }
}